
bt.module({builder: function(bt) {
    var j$ = jQuery;
    var current = 0;

    var _ellipsisHotDestnations = function() {
        var ellipsis_opts = {max_rows: 1};
        // hot_destinations name
        j$(".hot-destinations .photos .photo .mask .name").each(function() {
            if(!j$(this).attr("threedots")) {
                j$(this).ThreeDots(ellipsis_opts);
            }
        });
    };

    var _initLoadMoreEvent = function() {
        j$("#load-more").click(function() {
            var _this = this;
            j$('#ajax-more').css("visibility", "visible");

            j$.ajax({
                url: "/hot_destinations/",
                data: "start=" + j$(_this).data("start") + "&count=" + j$(_this).data("count"),
                success: function(resp) {
                    if(resp) {
                        j$(".hot-destinations .photos").append(resp);
                    } else {
                        j$("#load-more").slideUp();
                    }
                }
            }).done(function() {
                    _ellipsisHotDestnations();
                    j$(_this).remove();
                    _initLoadMoreEvent();
                });
        });
    };

    j$(document).ready(function() {
        _ellipsisHotDestnations();
        _initLoadMoreEvent();

        j$("#features .items .item").each(function() {
            j$(this).css("width", j$(window).width());
        });

        var clickdirection = 1;
        j$('.navi-next').click(function(){
            clickdirection = 1;
        });
        j$('.navi-prev').click(function(){
            clickdirection = 0;
        });
        j$(".scrollable").scrollable({circular: true,
                                      mousewheel: false,
                                      next: ".navi-next",
                                      prev: ".navi-prev"})
            .autoscroll({interval: 7000});
        var api = $(".scrollable").data("scrollable");
        //首页新增推广大图使用
        api.onBeforeSeek(function(e) {
            if ( ( clickdirection == 1 && this.getIndex() === -1 ) || ( clickdirection == 0 && this.getIndex() === 1 )) {
                $('#header').addClass('new-index-521');
            }else{
                $('#header').removeClass('new-index-521');
            }
        });

        j$(".index_v4 #features").bind("mouseover", function() {
            j$(this).find(".navi-prev").show();
            j$(this).find(".navi-next").show();
        });

        j$(".index_v4 #features").bind("mouseleave", function() {
            j$(this).find(".navi-prev").hide();
            j$(this).find(".navi-next").hide();
        });

        var api = j$(".scrollable").data("scrollable");
        api.onSeek(function() {
            current = this.getIndex();
            clickdirection = 1;
        });
        if (!j$.browser.msie || (j$.browser.msie && j$.browser.version > 8)) {
            var div = j$("<div></div>");
            div.addClass("extra-info");
            j$("#bg0").append(div);
        } else if (j$.browser.msie && j$.browser.version <= 8 && j$.browser.version != 6) {
            j$("#bg0 .bg").addClass("bg0-IE");
        }
    });

    j$(window).resize(function() {
        var window_w = j$(window).width();
        var min_w = j$("#content .center-align").width();
        if (window_w < min_w) {
            window_w = min_w;
        }
        j$("#features .items .item").each(function() {
            j$(this).css("width", window_w);
        });
        j$("#features .items").css("left", 0 - window_w * (current + 1));

    });

}});
